<template>
  <!-- 新增群 -->
  <div class="add-group">
    <div class="action">
      <div class="item flex-center" @click="onCancel">
        <span>取消</span>
      </div>
      <div class="item send flex-center" @click="onSend">
        <span>完成</span>
      </div>
    </div>
    <div class="input">
      <cube-input v-model="groupNick" placeholder="请填写群名称" />
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Prop } from "vue-property-decorator";
import MessageMixin from '@/mixins';
import { MsgTypeKV } from '@/api/socket/imClient';
import { Webim } from '@/utils/types';
declare const $Vue: Vue;
declare const webim: Webim;

@Component<AddGroup>({
  mixins: [MessageMixin],
})
export default class AddGroup extends Vue {
  // 表示映射chat模块下的mutation方法

  @Prop({})
  card: any;

  private groupNick: string = '';

  private eventList: any = ['$createGroup']

  protected mounted() {
    this.initEvents(this.eventList);
  }


  private onCancel() {
    this.$emit('hide');
    this.groupNick = '';
  }

  private onSend() {
    let groupNick = this.groupNick;
    if (groupNick.length > 10) {
      return this.$createToast({
        txt: '群名称长度不能超过10个字符',
        type: "warn",
        time: 3000
      }).show();
    }
    if (groupNick === '') {
      groupNick = '群聊';
    }
    
    this.sendMessage(MsgTypeKV.createGroup, {
      groupNick,
    });
  }
    /**
   * 申请加入群之后的返回结果处理
   */
  private $createGroup(data: any) {
    if (data.result === 0) {
      this.$createToast({
        txt: data.message,
        type: "correct",
        time: 3000
      }).show();
      this.sendMessage(MsgTypeKV.getGroupList, {
        signUrl: window.location.href
      });
      // 取消显示
      this.onCancel();
    } else {
      this.$createToast({
        txt: data.message,
        type: "warn",
        time: 3000
      }).show();
    }
  }
}
</script>

<style lang="stylus" scoped>
.add-group
  width 100%
  height 100%
  background #fff
  padding 2px 
  box-sizing border-box
  .icon
    flex-basis 10%
  
.action
  height 28px 
  width 100%
  display flex
  justify-content space-between
  align-items center
  padding 0 6px
  box-sizing border-box
  .input
    padding 0 8px
    padding-top 12px
  .item 
    width 48px
    height 100% 
    line-height 100% 
    &.send 
      background rgb(87, 189, 109)
      border-radius 4px
      color white

</style>

